package com.ww.springboot.boot.javabase.stream;

import java.util.*;
import java.util.stream.Collectors;

public class StreamDemo3 {
    public static void main(String[] args) {
        List<Order> orders = Arrays.asList(
                new Order(1, "Alice", 100),
                new Order(2, "Bob", 200),
                new Order(3, "Alice", 150),
                new Order(4, "Bob", 300),
                new Order(5, "Charlie", 50)
        );

        // 按照用户进行分组，求出每个用户的订单总金额
        //groupby 第一个值是分组键 第二个值是指定map类型 第三个值是对分组后的值进行处理
        Map<String, Integer> result = orders.stream()
                .collect(Collectors.groupingBy(Order::getCustomerName, Collectors.summingInt(Order::getTotalPrice)));
        System.out.println(result);
        TreeMap<String, Long> map = orders.stream()
                .collect(Collectors.groupingBy(Order::getCustomerName, TreeMap::new, Collectors.counting()));
        System.out.println(map);
        Map<String, Long> map2 = orders.stream()
                .collect(Collectors.groupingBy(Order::getCustomerName, Collectors.counting()));
        System.out.println(map2);
        Map<String, Integer> map3 = orders.stream()
                .collect(Collectors.groupingBy(Order::getCustomerName, Collectors.summingInt(Order::getTotalPrice)));
        System.out.println(map3);
        orders.stream()
                .collect(Collectors.groupingBy(Order::getCustomerName, LinkedHashMap::new, Collectors.counting()));
    }
}

class Order {
    private int id;
    private String customerName;
    private int totalPrice;

    public Order(int id, String customerName, int totalPrice) {
        this.id = id;
        this.customerName = customerName;
        this.totalPrice = totalPrice;
    }

    public int getId() {
        return id;
    }

    public String getCustomerName() {
        return customerName;
    }

    public int getTotalPrice() {
        return totalPrice;
    }
}
